package xu.zhi.qing.collect.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xu.zhi.qing.collect.entity.Collect;
import xu.zhi.qing.collect.service.CollectService;
import xu.zhi.qing.common.entity.ResponseJson;
import xu.zhi.qing.share.entity.Share;
import xu.zhi.qing.user.entity.User;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
@Scope("prototype")
@RequestMapping("/collect")
public class CollectController {
	
	@Autowired
	CollectService collectService;
	
	/**
	 * 添加收藏
	 * @param share 要收藏的share(只需要传id)
	 * @param session
	 * @return
	 */
	@RequestMapping("/addCollect")
	@ResponseBody
	public ResponseJson addCollect(Share share, HttpSession session){
		ResponseJson result=new ResponseJson();
		try {
			User user = (User)session.getAttribute("loginInfo");
			Collect collect=new Collect();
			collect.setCreateTime(new Timestamp(System.currentTimeMillis()));
			collect.setShareId(share.getId());
			collect.setUserId(user.getId());
			collectService.save(collect);
			result.setStatus(true);
			result.setMsg("收藏分享成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(true);
			result.setMsg("收藏分享失败！");
		}
		return result;
	}
	
	/**
	 * 取消收藏
	 * @param share	要取消收藏的share(只需要传id)
	 * @param session
	 * @return
	 */
	@RequestMapping("/deleteCollect")
	@ResponseBody
	public ResponseJson deleteCollect(Share share, HttpSession session){
		ResponseJson result=new ResponseJson();
		try {
			User user = (User)session.getAttribute("loginInfo");
			Collect collect=new Collect();
			collect.setShareId(share.getId());
			collect.setUserId(user.getId());
			collectService.delete(collect);
			result.setStatus(true);
			result.setMsg("取消分享成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(true);
			result.setMsg("取消分享失败！");
		}
		return result;
	}
	
}
